package leetcode.code0662;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	long ans;
	Long[][] map;

	public int widthOfBinaryTree(TreeNode root) {
		if (root == null) {
			return 0;
		}
		ans = 0;
		map = new Long[65][2];
		check(root, 1, 1);
		return (int) ans;
	}

	private void check(TreeNode root, int h, long index) {
		if (root == null) {
			return;
		}
		if (map[h][0] == null) {
			map[h] = new Long[] { index, index };
		} else {
			map[h][0] = Math.min(map[h][0], index);
			map[h][1] = Math.max(map[h][1], index);
		}
		ans = Math.max(ans, map[h][1] - map[h][0] + 1);
		this.check(root.left, h + 1, index << 1);
		this.check(root.right, h + 1, (index << 1) + 1);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		System.out.println(Long.toBinaryString(Long.MAX_VALUE).length());
	}

}
